﻿using System.ComponentModel;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;

namespace CodeAgent.Host.Functions;

public class HttpFunction
{
    private readonly HttpClient _httpClient;

    public HttpFunction(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    [KernelFunction, Description("发送GET请求")]
    public async Task<string> GetAsync(
        [Description("请求URL")] string url
    )
    {
        var response = await _httpClient.GetAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }

    [KernelFunction, Description("发送POST请求")]
    public async Task<string> PostAsync(
        [Description("请求URL")] string url,
        [Description("请求内容")] string content
    )
    {
        var response = await _httpClient.PostAsync(url, new StringContent(content));
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }

    [KernelFunction, Description("发送PUT请求")]
    public async Task<string> PutAsync(
        [Description("请求URL")] string url,
        [Description("请求内容")] string content
    )
    {
        var response = await _httpClient.PutAsync(url, new StringContent(content));
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }

    [KernelFunction, Description("发送DELETE请求")]
    public async Task<string> DeleteAsync(
        [Description("请求URL")] string url
    )
    {
        var response = await _httpClient.DeleteAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}